import { isUrl } from '../utils/utils';

const menuData = [
    {
        name: '账户',
        icon: 'user',
        path: 'user',
        hideInMenu: true,
        children: [
            {
                name: '登录',
                path: 'login',
            },
        ],
    },
    {
        name: '用户管理',
        path: 'user-list',
        authority: ['1']
    },
    {
        name: '交易管理',
        path: 'transaction-list',
        authority: ['1']
    }
];

function formatter(data, parentPath = '/', parentAuthority) {
    return data.map(item => {
        let { path } = item;
        if (!isUrl(path)) {
            path = parentPath + item.path;
        }
        const result = {
            ...item,
            path,
            authority: item.authority || parentAuthority,
        };
        if (item.children) {
            result.children = formatter(
                item.children,
                `${parentPath}${item.path}/`,
                item.authority
            );
        }
        return result;
    });
}

/**
// 递归向上合并Authority
function format(data=[],parentPath=''){
  return data.map(item=>{
    let { path } = item
    path =`${parentPath}/${path}`
    const children = format(item.children,item.path,item.authority)
    const authority = (children && Array.from(new Set(children.reduce((acc,next)=>{
      return next.authority ? acc.concat(next.authority): acc.concat([])
    }
      ,item.authority||[]))))

    const result = {
      ...item,
      path,
      authority,
      children,
    }
    return result
  })
}
 */
export const getMenuData = () => formatter(menuData);
